<template>
    <div class="card">
        <div class="card-body">
            <div class="row justify-content-md-center">
                <div class="col-3">
                    <form @submit.prevent="login">
                        <div class="mb-3">
                            <label class="form-label">用户名</label>
                            <input type="text" class="form-control" v-model="username">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">密码</label>
                            <input type="password" class="form-control" v-model="password">
                        </div>
                        <div class="error_message">{{ error_message }}</div>
                        <button type="submit" class="btn btn-primary">登录</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { ref } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
export default {
    name: "LoginView",
    setup() {
        const store = useStore();
        const username = ref('');
        const password = ref('');
        const error_message = ref('');
        const router = useRouter();
        const login = () => {
            error_message.value = "";
            store.dispatch("login", {
                username: username.value,
                password: password.value,
                success() {
                    router.push({ name: 'home' });
                },
                error() {
                    error_message.value = "学号或密码错误";
                }
            })
        }
        return {
            username,
            password,
            error_message,
            login
        }
    }
}
</script>

<style scoped>
.btn-primary {
    width: 100%;
}
.error_message {
    color: red;
}
</style>